package com.itheima.demo02;

public class MainClass05 {
    public static void main(String[] args) {
        int[] arr = {3, 1, 8, 6, 5, 9, 7};

        useQuicksort(arr, 0, arr.length - 1);
        usePrint(arr);
    }

    private static void usePrint(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    private static void useQuicksort(int[] arr, int left, int right) {
        //设置递归出口，当右边索引大于左边索引时，递归结束
        if (right < left){
            return;
        }
        int left0 = left;
        int right0 = right;
        int Basenum = arr[left0];

        //先用基准值和最右边的数进行比较，如果最右边数小于它，则right--，继续进行比较，直到遇到比基准值小的数停下
        while (left != right){
            while (Basenum <= arr[right] && right > left){
                right--;
            }
            while (Basenum >= arr[left] && right > left){
                left++;
            }
            int temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
        }
        int temp = arr[left];
        arr[left] = arr[left0];
        arr[left0] = temp;

        useQuicksort(arr,left0,left - 1);
        useQuicksort(arr,left + 1 ,right0);
    }
}
